package com.lcj.controller;

import com.lcj.entity.FileDownloadLog;
import com.lcj.service.FileDownloadLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/file")
@Tag(name = "文件日志下载服务")
public class FileDownloadLogController {

    @Resource
    private FileDownloadLogService fileDownloadLogService;

    @Operation(summary = "日志列表")
    @GetMapping("/list")
    public List<FileDownloadLog> list() {
        log.info("list()线程: {}", Thread.currentThread());
        return fileDownloadLogService.list();
    }

    @Operation(summary = "日志详情")
    @Parameter(name = "id", description = "日志id", required = true, in = ParameterIn.PATH)
    @GetMapping("/detail/{id}")
    public FileDownloadLog detail(@PathVariable("id") Long id) {
        log.info("detail()线程: {}", Thread.currentThread());
        return fileDownloadLogService.getById(id);
    }

    @Operation(summary = "日志更新")
    @PostMapping("save")
    public FileDownloadLog save(@RequestBody FileDownloadLog fileDownloadLog) {
        log.info("save()线程: {}", Thread.currentThread());
        // 填补数据
        Date date = new Date();
        if (Objects.isNull(fileDownloadLog.getId())) {
            fileDownloadLog.setAddTime(date);
        }
        fileDownloadLog.setUpdateTime(date);
        fileDownloadLogService.saveOrUpdate(fileDownloadLog);
        return fileDownloadLog;
    }
}